package gao.xiaolei.controller;

import java.util.List;
import javax.annotation.Resource;

import gao.xiaolei.dto.TestCheckCreateDto;
import gao.xiaolei.dto.TestCheckDetailDto;
import gao.xiaolei.dto.TestCheckDto;
import gao.xiaolei.dto.TestCheckLogDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.service.TestRequirementCheckService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testRequirementCheck")
@CrossOrigin
public class TestRequirementCheckController {

    @Resource(name = "TestRequirementCheckService")
    private TestRequirementCheckService testRequirementCheckService;

    //获取审核详情
    @GetMapping("/detail/{status}/{id}")
    public TestCheckDetailDto getDetail(@PathVariable("id") String id,
                                        @PathVariable("status") Integer status) {
        return testRequirementCheckService.getDetail(id, status);
    }

    //给一个测试需求添加一个审核记录
    @PostMapping("")
    public void addCheck(@RequestBody List<TestCheckCreateDto> testRequirementChecks) {
        testRequirementCheckService.addCheck(testRequirementChecks);
    }

    // 删除一个审核记录
    @DeleteMapping("/{id}")
    public ResponseEnvelope<String> deleteCheck(@PathVariable("id") String id) {
        testRequirementCheckService.deleteCheck(id);
        return new ResponseEnvelope<String>(200, "success", null);
    }

    //获取某个审核人在某个项目的审核记录
    @GetMapping("/checkMan/{projectId}/{checkMan}/{status}")
    public List<TestCheckDto> getByCheckManIsFinish(@PathVariable("projectId") String projectId,
                                                    @PathVariable("checkMan") String checkMan,
                                                    @PathVariable("status") Integer status) {
        if (status == 1)//如果status==1就去获取已经审核完的审核记录
            return testRequirementCheckService.findByCheckManAndFinshed(checkMan, projectId);
        else if (status == 0)//如果status==0就去获取开始审核但是还没审核完的审核记录
            return testRequirementCheckService.findByCheckManAndNotFinshed(checkMan, projectId);
        else //如果status等于-1就去拿待审核的测试需求
            return testRequirementCheckService.getReadyToCheck(projectId);
    }

    //获取某个审核节点下的子审核
    @GetMapping("/children/{status}/{id}")
    public ResponseEnvelope<List<TestCheckDto>> getChildren(@PathVariable("id") String id,
                                                            @PathVariable("status") Integer status) {
        if (status == -1)
            return new ResponseEnvelope<List<TestCheckDto>>(200, "success", testRequirementCheckService.getTestRequirementChildren(id));
        else
            return new ResponseEnvelope<List<TestCheckDto>>(200, "success", testRequirementCheckService.getTestCheckChildren(id));
    }

    //获取某个测试需求的所有审核记录
    @GetMapping("/common/{testReqirementId}")
    public ResponseEnvelope<List<TestCheckLogDto>> getByRequirement(@PathVariable("testReqirementId") String testReqirementId) {
        return new ResponseEnvelope<List<TestCheckLogDto>>(200, "success", testRequirementCheckService.findByTestRequirement(testReqirementId));
    }

    //获取某个测试需求的所有审核次数
    @GetMapping("/common/count/{testReqirementId}")
    public ResponseEnvelope<Integer> getCountByRequirement(@PathVariable("testReqirementId") String testReqirementId) {
        return new ResponseEnvelope<Integer>(200, "success", testRequirementCheckService.getConutByTestRequirement(testReqirementId));
    }

    //获取审核批注
    @GetMapping("/common/annotate/{id}")
    public ResponseEnvelope<String> getAnnotate(@PathVariable("id") String id) {
        return new ResponseEnvelope<String>(200, "success", testRequirementCheckService.getAnnotate(id));
    }

    //修改审核是否通过和审核批注
    @PutMapping("/annoAndIsPass/{isFinish}")
    public void updateAnnotateAndIsPass(@RequestBody List<TestCheckCreateDto> testRequirementChecks,
                                        @PathVariable("isFinish") Integer isFinish) {
        testRequirementCheckService.updateAnnotateAndIsPass(testRequirementChecks, isFinish);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
